package edu.zzuli.assistant.until;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 文件工具类
 * 
 */
public class FileUtil {
	
	public static String VERSION_PATH = "d:\\12.txt";        // app版本路径
	public static String VERSION_PATH_TEMP = "d:\\123.txt";  //app版本临时路径
	
	public static double getDirSize(File file) {     
        //判断文件是否存在     
        if (file.exists()) {     
            //如果是目录则递归计算其内容的总大小    
            if (file.isDirectory()) {     
                File[] children = file.listFiles();     
                double size = 0;     
                for (File f : children)     
                    size += getDirSize(f);     
                return size;     
            } else {//如果是文件则直接返回其大小,以“兆”为单位   
                double size = (double) file.length() / 1024 / 1024;        
                return size;     
            }     
        } else {     
            return 0.0;     
        }     
    }
	
	// 新建文件夹
	public static boolean  makeDir(String path){
		
		File file = new File(path);
		if(!file.exists()){
			
			file.mkdirs();// 创建文件夹
		}
		return true;
	}
	
	// 删除文件
	public static boolean delete(String path){
		
		File file = new File(path);
		if(file.exists()){
			
			file.delete();//删除文件
		}
		return true;
	}
	// 删除文件
	public static boolean reName(String path,String newName){
		System.out.println( path);
		System.out.println( newName);
		File file = new File(path);
		File newFile = new File(newName);
		return file.renameTo(newFile);
	}
	/**
	 * 写入文件到磁盘
	 * @param path
	 * @param content
	 */
	public static void writeToText(String path, String content) {

		File f = new File(path);
		BufferedOutputStream bos = null;
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(f);
			bos = new BufferedOutputStream(outputStream);
			byte[] contentInBytes = content.getBytes();
			bos.write(contentInBytes);
			bos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(bos!=null){
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(outputStream!=null){
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	public static String  readText(String path) {
		
		FileReader filereader=null;
		try {
			filereader = new FileReader(path);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BufferedReader bufferedReader=new BufferedReader(filereader);
		String str="";
		StringBuffer buffer = new StringBuffer();
		try {
			while((str=bufferedReader.readLine())!=null){
				buffer.append(str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(bufferedReader!=null){
				
				try {
					bufferedReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(filereader!=null){
				
				try {
					filereader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return buffer.toString();
	}

	public static void main(String[] args) {
		writeToText(VERSION_PATH_TEMP,"dgdughsdg你好啊回复该广告呵呵呵呵呵呵呵呵刚合适的房价更好的是否经过和第三方机构的房管局的符合规定发几个"); //写入文件
		delete(VERSION_PATH);                            //删除旧版本
		reName(VERSION_PATH_TEMP,VERSION_PATH);          //改名字
		Long start = System.currentTimeMillis();
		System.out.println(readText(VERSION_PATH));
		System.out.println(System.currentTimeMillis()-start);
	}
}
